package client.popups.appointment;

import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

import javax.swing.JLabel;
import net.miginfocom.swing.MigLayout;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

import client.popups.wizard.AppointmentsRoomDescription;
import data.Appointment;
import data.PropertyChangeSpeaker;
import data.room.Room;

public class RoomOverview extends JFrame implements PropertyChangeSpeaker{
	private JLabel selectedRoom;
	private AppointmentsRoomDescription ard;
	private JButton removeButton;
	private JButton selectButton;
	
	private PropertyChangeSupport pcs;
	
	private Room room;
	
	public RoomOverview(Room r, Appointment a) {
		setIconImage(Toolkit.getDefaultToolkit().getImage("resources/icons/aCalendarIcon.png"));
		pcs = new PropertyChangeSupport(this);
		room = r;
		ard = new AppointmentsRoomDescription();
		if(a.getReservation().getStartTime() != null && 
				a.getReservation().getEndTime() != null) {
			
			ard.updateAvailRooms(a.getReservation().getStartTime().getTimeInMillis(), 
					a.getReservation().getEndTime().getTimeInMillis());
		} else {
			JOptionPane.showMessageDialog(null, "Kunne ikke lase inn rom da avtalen ikke har tidspunkt");
		}
		
		JPanel panel = new JPanel();
		panel.setLayout(new MigLayout("wrap 2", "[]", "[][][]"));
		
		JLabel roomLabel = new JLabel("Valgt rom:");
		panel.add(roomLabel, "flowx,cell 0 0");

		selectedRoom = new JLabel();
			selectedRoom.setPreferredSize(new Dimension(60, 20));
			selectedRoom.setBackground(Color.WHITE);
			selectedRoom.setBorder(BorderFactory.createLineBorder(Color.BLACK));
			selectedRoom.setOpaque(true);
			if(room != null){
				selectedRoom.setText(room.getName());
			} else {
				selectedRoom.setText("");
			}
			panel.add(selectedRoom, "cell 0 0");
		
			panel.add(ard, "cell 0 1");
		
		removeButton = new JButton("Fjern rom");
			removeButton.addActionListener(new ButtonActionListener());
		panel.add(removeButton, "flowx,cell 0 2,width 90!");
		
		selectButton = new JButton("Velg rom");
			selectButton.addActionListener(new ButtonActionListener());
		panel.add(selectButton, "cell 0 2");
		
		this.add(panel);
	}

	public void addPropertyChangeListener(PropertyChangeListener listener){
		pcs.addPropertyChangeListener(listener);
	}

	public void removePropertyChangeListener(PropertyChangeListener listener){
		pcs.removePropertyChangeListener(listener);
	}
	public class ButtonActionListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			if(e.getSource() == selectButton){
				Room r = (Room) ard.getRoomList().getSelectedValue();
				if(r != null){
					room = r;
					selectedRoom.setText(r.getName());
					pcs.firePropertyChange("overviewUpdate", null, r);
					dispose();
				}
			}else if(e.getSource() == removeButton){
				selectedRoom.setText("");
				room = null;
				pcs.firePropertyChange("overviewUpdate", null, null);
				dispose();
			}
		}
		
	}
}
